#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import sc2reader 
import sc2reader.objects
import sc2reader.resources
import sc2reader.data
import sc2reader.events
import inspect
from sc2reader import SC2Reader
from ugly_lookup import TOSS_LOOKUP
from ugly_lookup import * 

import dircache

SUPPLY_BUCKETS = 8
def do(path):
    print "Beginning process"
    game_id = 0
    GAME_DB_HANDLE = open( "GAME_DB.txt", "w")
    replays = set()
    file_list = dircache.listdir(path)

    for file in file_list:
        replay = SC2Reader().load_replay( "{0}\{1}".format( path, file ) )
        
        game_id = game_id+1
        
        print "Release {0}".format(replay.release_string)
        print "{0} on {1}".format(replay.type,replay.map)
        by_player = {}
        for player in replay.players:
            by_player[player] = {0:4,1:'',2:''}
            print player
        print "\n--------------------------\n\n"
        
        events = replay.events
        # Loop through the events
        lookup = TOSS_LOOKUP.copy()
        
        empty_bcode = BCode('UNKNOWN','')
        
        for event in events:
            if( isinstance( event, sc2reader.events.AbilityEvent ) ):
               
                build_code = lookup[event.ability] if event.ability in lookup else empty_bcode
                if not event.ability in lookup : continue
                
                player = event.player
                target = event.target if hasattr(event, 'target') else ""
                location = event.location if hasattr( event, 'location') else ""
                # I think bucketing by food is better than time
                food = TOSS_FOOD_LOOKUP[build_code.code] if build_code.code in TOSS_FOOD_LOOKUP else 0
                old_food = by_player[player][0] 
                by_player[player][0] = new_food = old_food + food
                
                bo_addition = build_code.code if not event.ability in TOSS_EXCLUDE_LIST else ''
                
                by_player[player][1] += bo_addition
                
                if old_food / SUPPLY_BUCKETS != new_food / SUPPLY_BUCKETS:
                    by_player[player][1] += " {0} {1} ".format( new_food, event.time.seconds)
                by_player[player][2] += "{0},{1},{2},{3},{4},{5},{6}\r".format( event.time.seconds, player.name, hex( event.ability ), build_code.text, food, target, location)
                
                if new_food >= 60: break
                
        
        build_file_format = "parsed\\BO.{0}.{1}.txt"
        for player in replay.players:
            print by_player[player][1]
            
            BO_HANDLE = open( build_file_format.format(game_id,player.name), "w") 
            BO_HANDLE.write( "\r" + by_player[player][1] )
            BO_HANDLE.write( by_player[player][2] )
            
            BO_HANDLE.flush()
            if len(by_player[player][1]) >0:
                GAME_DB_HANDLE.write("{0}:{1}:{2}\r".format( game_id, player.name, by_player[player][1] ) )
            BO_HANDLE.close()
            
    GAME_DB_HANDLE.flush()
    GAME_DB_HANDLE.close()
    
if __name__ == '__main__':

    do(sys.argv[1])
